package com.wjc.server.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjc.server.dto.RoleUserDto;
import com.wjc.server.dto.PageDto;
import com.wjc.server.entity.RoleUser;
import com.wjc.server.entity.RoleUserExample;
import com.wjc.server.mapper.RoleUserMapper;
import com.wjc.server.util.CopyUtil;
import com.wjc.server.util.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class RoleUserService {
    @Resource
    private RoleUserMapper roleUserMapper;

    @Resource
    private SnowFlake snowFlake;

    //查询
    public void list(PageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        RoleUserExample roleUserExample = new RoleUserExample();
        List<RoleUser> roleUserList= roleUserMapper.selectByExample(roleUserExample);
        PageInfo<RoleUser> pageInfo=new PageInfo<>(roleUserList);
        pageDto.setTotal(pageInfo.getTotal());

        List<RoleUserDto> roleUserDtosList = CopyUtil.copyList(roleUserList,RoleUserDto.class);
        pageDto.setList(roleUserDtosList);

    }

    //保存
    public void save(RoleUserDto roleUserDto){

        RoleUser roleUser=CopyUtil.copy(roleUserDto,RoleUser.class);
        if (ObjectUtils.isEmpty(roleUser.getId())){
            //新增
            roleUser.setId(snowFlake.nextId());
            roleUserMapper.insert(roleUser);
        }else {
            roleUserMapper.updateByPrimaryKey(roleUser);
        }

    }

    public void delete(Long id){
        roleUserMapper.deleteByPrimaryKey(id);
    }
}
